<?php
/**
 * Group edit form
*
* @package ElggGroups
*/

// new groups default to open membership
if (isset ( $vars ['entity'] )) {
	$membership = $vars ['entity']->membership;
	$access = $vars ['entity']->access_id;
	if ($access != ACCESS_PUBLIC && $access != ACCESS_LOGGED_IN) {
		// group only - this is done to handle access not created when group is created
		$access = ACCESS_PRIVATE;
	}
} else {
	$membership = ACCESS_PUBLIC;
	$access = ACCESS_PUBLIC;
}
$name = elgg_echo ( "groups:name" );
if (isset ( $vars ['typeadd'] ) && $vars ['typeadd'] == SUBJECT_GROUP) {
	$name = "Tên môn học";
}

?>
<div>
	<label><?php echo elgg_echo("groups:icon"); ?></label><br />
	<?php echo elgg_view("input/file", array('name' => 'icon')); ?>
</div>
<div>
	<label><?php echo $name; ?></label><br />
	<?php
	
	echo elgg_view ( "input/text", array (
			'name' => 'name',
			'value' => $vars ['entity']->name 
	) );
	?>
</div>
<?php

$group_profile_fields = elgg_get_config ( 'group' );
if ($group_profile_fields > 0) {
	foreach ( $group_profile_fields as $shortname => $valtype ) {
		$line_break = '<br />';
		if ($valtype == 'longtext') {
			$line_break = '';
		}
		echo '<div><label>';
		echo elgg_echo ( "groups:{$shortname}" );
		echo "</label>$line_break";
		echo elgg_view ( "input/{$valtype}", array (
				'name' => $shortname,
				'value' => $vars ['entity']->$shortname 
		) );
		echo '</div>';
	}
}
?>
<?php
// ------------ Toan Nguyen ------------------ //
if (isset ( $vars ['typeadd'] )) {
	$grouptype = elgg_view ( 'input/hidden', array (
			'name' => 'grouptype',
			'value' => $vars ['typeadd'] 
	) );
	echo $grouptype;
}

?>

<div>
	<label>
		<?php echo elgg_echo('groups:membership'); ?><br />
	</label>
	<?php
	
	echo elgg_view ( 'input/dropdown', array (
			'name' => 'membership',
			'value' => $membership,
			'options_values' => array (
					ACCESS_PRIVATE => elgg_echo ( 'groups:access:private' ),
					ACCESS_PUBLIC => elgg_echo ( 'groups:access:public' ) 
			) 
	) );
	?>
</div>

<?php

if (elgg_get_plugin_setting ( 'hidden_groups', 'groups' ) == 'yes') {
	$this_owner = $vars ['entity']->owner_guid;
	if (! $this_owner) {
		$this_owner = elgg_get_logged_in_user_guid ();
	}
	$access_options = array (
			ACCESS_PRIVATE => elgg_echo ( 'groups:access:group' ),
			ACCESS_LOGGED_IN => elgg_echo ( "LOGGED_IN" ),
			ACCESS_PUBLIC => elgg_echo ( "PUBLIC" ) 
	);
	?>

<div>
	<label>
			<?php echo elgg_echo('groups:visibility'); ?><br />

	</label>
	<?php
	echo elgg_view ( 'input/access', array (
			'name' => 'vis',
			'value' => $access,
			'options_values' => $access_options 
	) );
	?>
</div>

<?php
}

if (isset ( $vars ['entity'] )) {
	$entity = $vars ['entity'];
	$owner_guid = $vars ['entity']->owner_guid;
} else {
	$entity = false;
}

if ($entity && ($owner_guid == elgg_get_logged_in_user_guid () || elgg_is_admin_logged_in ())) {
	$owner_guid = $vars ['entity']->owner_guid;
	$members = array ();
	foreach ( $vars ['entity']->getMembers ( 0 ) as $member ) {
		$members [$member->guid] = "$member->name (@$member->username)";
	}
	?>

<div>
	<label>
			<?php echo elgg_echo('groups:owner'); ?><br />

	</label>
	
	<?php
	echo elgg_view ( 'input/dropdown', array (
			'name' => 'owner_guid',
			'value' => $owner_guid,
			'options_values' => $members,
			'class' => 'groups-owner-input' 
	) );
	if ($owner_guid == elgg_get_logged_in_user_guid ()) {
		echo '<span class="elgg-text-help">' . elgg_echo ( 'groups:owner:warning' ) . '</span><br>';
	}
	?>
</div>

<?php
}

$tools = elgg_get_config ( 'group_tool_options' );
if ($tools) {
	usort ( $tools, create_function ( '$a,$b', 'return strcmp($a->label,$b->label);' ) );
	// ----------- Toan Nguyen ---------------- //
	echo elgg_view ( 'output/url', array (
			'href' => "#",
			'text' => elgg_echo ( 'group:moresetting' ),
			'onclick' => "toggle('group_moresetting'); return false;" 
	) ) . "</br>";
	
	echo "<br><div id='group_moresetting' style='display:none;'>";
	foreach ( $tools as $group_option ) {
		$group_option_toggle_name = $group_option->name . "_enable";
		if ($group_option->default_on) {
			$group_option_default_value = 'yes';
		} else {
			$group_option_default_value = 'no';
		}
		$value = $vars ['entity']->$group_option_toggle_name ? $vars ['entity']->$group_option_toggle_name : $group_option_default_value;
		?>
<div>
	<label>
		<?php echo $group_option->label; ?><br />
	</label>
		<?php
		
		echo elgg_view ( "input/radio", array (
				"name" => $group_option_toggle_name,
				"value" => $value,
				'options' => array (
						elgg_echo ( 'groups:yes' ) => 'yes',
						elgg_echo ( 'groups:no' ) => 'no' 
				) 
		) );
		?>
</div>
<?php
	}
	echo "</div></br>";
}
?>
<div class="elgg-foot">
<?php

if (isset ( $vars ['entity'] )) {
	echo elgg_view ( 'input/hidden', array (
			'name' => 'group_guid',
			'value' => $vars ['entity']->getGUID () 
	) );
}

echo elgg_view ( 'input/submit', array (
		'value' => elgg_echo ( 'save' ) 
) );

if (isset ( $vars ['entity'] )) {
	$delete_url = 'action/groups/delete?guid=' . $vars ['entity']->getGUID ();
	echo elgg_view ( 'output/confirmlink', array (
			'text' => elgg_echo ( 'groups:delete' ),
			'href' => $delete_url,
			'confirm' => elgg_echo ( 'groups:deletewarning' ),
			'class' => 'elgg-button elgg-button-delete float-alt' 
	) );
}
?>
</div>

<script type="text/javascript">
	function toggle(project_group){
		$("#"+project_group).toggle();
	}
</script>